﻿public interface Ishape {
    bool isLawful();
    float getArea();
}

public class Rectangle : Ishape
{
    public int x { set; get; }
    public int y { set; get; }
    public Rectangle(int x,int y)
    {
        this.x = x;
        this.y = y;
    }
    public float getArea()
    {
        return x * y;
    }

    public bool isLawful()
    {
        return (x > 0 && y > 0 && x!=y);
    }
}

public class Square : Ishape
{
    public int x { set; get; }
    public Square(int x)
    {
        this.x = x;
    }
    public float getArea()
    {
        return x * x;
    }
    public bool isLawful()
    {
        return x > 0;
    }
}

public class Circle : Ishape {
    public int r { set; get; }
    public Circle(int r)
    {
        this.r = r;
    }
    public float getArea()
    {
        return r * r * (float)3.14;
    }

    public bool isLawful()
    {
        return r> 0;
    }
}

public class Triangle : Ishape{
	int a,b,c;

    public Triangle(int a, int b, int c){
        this.a = a;
        this.b = b;
        this.c = c;
    }
    public float getArea()
    {
        float p = (a + b + c) / 2;
        return (float)Math.Sqrt(p * (p - a) * (p - b) * (p - c));
    }

    public bool isLawful()
    {
        throw new NotImplementedException();
    }
}

public class ConsoleApp1
{
    public static Ishape CreateShape()
    {
        try
        {
            object? sp = null;
            int j = new Random().Next(4);
            if (j == 0)
            {
                sp = new Circle(new Random().Next(100));
            }

            else if (j == 1)
            {
                sp = new Rectangle(new Random().Next(100), new Random().Next(100));
            }
            else if (j == 2)
            {
                int a, b, c;
                a = new Random().Next(100);
                b = new Random().Next(100);
                c = new Random().Next(100);
                sp = new Triangle(a, b, c);
            }
            else if (j == 3)
            {
                sp = new Square(new Random().Next(100));
            }
            return (Ishape)sp;
        }
        catch (Exception e)
        {
            throw new Exception();
        }
    }

    public static void Main(String[] args)
    {
        ConsoleApp1 ot = new ConsoleApp1();
        float areaSum = 0;
        for (int i = 0; i < 10; i++)
        {
            object a = CreateShape();
            Console.WriteLine("第" + (i + 1) + "个形状是：");
            if (a is Circle){
                Console.WriteLine("圆形");
            }
            else if (a is Rectangle){
                Console.WriteLine("长方形");
            }

            else if (a is Triangle){
                Console.WriteLine("三角形");
            }
            else if (a is Square){
                Console.WriteLine("正方形");
            }
            areaSum += ((Ishape)a).getArea();
        }
        Console.WriteLine("这些对象的面积总和为"+areaSum);
    }
}
